<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>City</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <script src="layui/layui.js"></script>
</head>
<body>
    <!--添加按钮-->
    <button type="button" class="layui-btn" id="add">添加城市</button>
    <!--添加form表单-->
    <form class="layui-form" id="addForm" lay-filter="addFormFilter" style="display: none;padding: 20px 20px 10px 10px"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
        <div class="layui-form-item">
            <label class="layui-form-label">城市名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">GDP总值</label>
            <div class="layui-input-block">
                <input type="text" name="gdb" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属省份</label>
            <div class="layui-input-block">
                <select id="addSelect" name="province.pid" lay-filter="addSelectFilter">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="addFilter">添加</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <!-- 条件搜索 -->
    <div class="layui-row">
        <form class="layui-form layui-form-pane" id="searchForm" lay-filter="searchFormFilter">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" id="name" name="name" autocomplete="off"
                               placeholder="请填写城市名称"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-sm" id="searche" lay-submit="" lay-filter="searchFilter">搜索</button>
                </div>
            </div>
        </form>
    </div>
    <!--展示table表格-->
    <table id="myTable" lay-filter="tableFilter"></table>
    <!--toolbar组件-->
    <script type="text/html" id="bar">
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
    </script>
    <!--layui代码-->
    <script>
        layui.use(['table','form','layer'],function () {
            var table = layui.table;
            var form = layui.form;
            var layer = layui.layer;
            var $ = layui.$;
            //渲染表格
            table.render({
                elem:'#myTable',
                url:'/springboot-homework/city/show',
                height:'312px',
                page:true,
                limit:3,
                limits:[3,4,5],
                cols:[[
                    {field:'id',title:'城市编号',width:180,sort:true},
                    {field:'name',title:'城市名称',width:180,sort:true},
                    {field:'gdb',title:'GDP',width:180,sort:true},
                    {title:'所属省份',width:180,templet:function (d) {
                            return d.province.pname
                        }},
                    {title:'操作',toolbar:'#bar'}
                ]]
            })
            //添加
            //加载省份下拉框数据
            $.ajax({
                url:'/springboot-homework/province/select',
                success:function (result) {
                    var s = '';
                    $(result).each(function (i,obj) {
                        s+='<option value="'+obj.pid+'">'+obj.pname+'</option>'
                    })
                    $('#addSelect').html(s);
                    //刷新下拉框  因为下拉框数据是动态更新的
                    form.render('select');
                }
            })
            $('#add').click(function () {
                //弹出添加页面
                layer.open({
                    type:1,
                    title:'添加页面',
                    area:'500px',
                    content:$('#addForm')
                })
                //提交添加表单数据
                form.on('submit(addFilter)',function () {
                    $.ajax({
                        url:'/springboot-homework/city/add',
                        data:$('#addForm').serialize(),
                        success:function (result) {
                            if(result.flag){
                                layer.msg('添加成功');
                                layer.closeAll();
                                $('#addForm')[0].reset();
                                table.reload('myTable');
                            }
                        }
                    })
                    //阻止表单提交
                    return false;
                })
            })
            //搜索展示
            $('#searche').click(function () {
                var a1 = $('#name').val();
                console.log(a1);
                table.reload('myTable',{
                    where:{
                        name:$('#name').val()
                    },
                    page:{
                        curr:1
                    }
                })
            })
            //删除
            table.on('tool(tableFilter)',function (obj){
              if(obj.event=='del'){
                  layer.confirm('确定删除吗？',function (index) {
                      $.ajax({
                          url:'/springboot-homework/city/remove',
                          data:'id='+obj.data.id,
                          success:function (result) {
                              if(result.flag){
                                  layer.msg('删除成功');
                                  table.reload('myTable')
                              }
                          }
                      })
                      layer.close(index)
                  })
              }
            })
        })
    </script>
</body>
</html>